/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treegenerator.arbaro;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author jannes
 */
public class LeafTextureCreator extends StemTextureCreator{
    @Override
    public BufferedImage createImage(){   
        BufferedImage bi = null;
        if(!tiles.isEmpty()){
            int side = sideCount();
            bi = new BufferedImage(tileSize * side, tileSize * side, BufferedImage.TYPE_INT_ARGB);        
            Graphics2D g = bi.createGraphics();
            int offset = (int)seed;
            for(int x = 0; x < side; x++){
                for(int y = 0; y < side; y++){                
                    BufferedImage tile = tiles.get((x+(side*y)+offset)%tiles.size());
                    g.drawImage(tile, null, tileSize*x, tileSize*y);
                }
            }        
        }
        else{            
            bi = new BufferedImage(128,128,BufferedImage.TYPE_INT_ARGB);            
            Graphics g = bi.getGraphics(); 
            g.setColor(Color.GREEN);
            g.fillRect(0, 0, 128, 128);
        }
        return bi;
    }
    
    @Override
    public BufferedImage createImage(long seed){
        return createImage();
    }
    
    public int sideCount(){
        int side = 1;
        while((side*side) < tiles.size()){
            side++;
        }  
        return side;
    }
    
    @Override
    public Material createMaterial(BufferedImage bi,AssetManager assetManager){
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");                         
        Texture texture = new Texture2D(awtLoader.load(bi, false));                
        mat.setTexture("ColorMap", texture);
        //mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        //mat.getAdditionalRenderState().setDepthWrite(false);
        return mat;
    }
}
